#! /bin/bash
#
# test-yacas -- Script for testing Yacas

# Give help, if requested

if [ $# -eq 0 ] || [ "x$1" = "x-h" ] || [ "x$1" = "x--help" ]; then
    echo "Usage: $0 <cmd> <dir> <script>..."
    echo "  cmd       Command plus options, needed to run Yacas"
    echo "  dir       Directory in which scripts reside"
    echo "  script... Test scripts to be run"
    echo "Test script may reside in <dir> or in the current directory"
    echo "Exit status is number of tests scripts which fail"
    echo "Example:"
    echo "  $0 \"../src/Debug/yacas --archive ../src/scripts.dat\" . short*.yts"
    exit 0
fi

# Parse arguments

if [ $# -lt 2 ]; then
    echo "Error: $0: Missing parameters"
    exit 255
fi

CMD="$1"
SCRIPTDIR="$2"
shift
shift
SCRIPTS=$*

# run the tests

FAILED_TESTS=""  # list of failed tests
FAILURES=0       # number of failed tests
TOTALTESTS=0     # total number of tests

TESTFILE=/tmp/test-yacas.$$
TIMEFILE=/tmp/time-yacas.$$
VERSIONF=/tmp/version-yacas-$$.ys
LOGFILE=yacas-logfile.txt
echo "Print(Version());" > $VERSIONF
VERSION=`$CMD $VERSIONF`
COMMENT=`$CMD $VERSIONF`
#DATECMD="date \"+%F %T\""
#echo "$DATECMD"
#rm $VERSIONF

echo "/*" >> $LOGFILE
echo "Command = \"$CMD\""      | tee -a $LOGFILE
echo "Version = $VERSION"      | tee -a $LOGFILE
echo "Comment = $COMMENT"      | tee -a $LOGFILE
echo "Date = \"`date "+%F %T"`\""   | tee -a $LOGFILE
echo "Logfile = \"$LOGFILE\""  | tee -a $LOGFILE
echo "*/" >> $LOGFILE

REALTOT="0"
USERTOT="0"
SYSSTOT="0"

for scr in $SCRIPTS; do
    if [ -f $SCRIPTDIR/$scr ]; then
	   f=$SCRIPTDIR/$scr
    else
	   f=$scr
    fi
#		if grep -i "interrupt" $TESTFILE > /dev/null; then
#			PASSFAIL=Interrupted
#		fi
    echo "Running $scr"
	if [ -f $TESTFILE ]; then rm $TESTFILE ; fi
    /bin/bash -c "time -p ($CMD $f || echo \"Error: exit status $?\") | tee $TESTFILE" \
		2> $TIMEFILE \
		|| (echo "Error -- User interrupt" > $TESTFILE)
#	cat $TIMEFILE
    if grep -E "\*\*\*\*\*\*|Error|interrupt" $TESTFILE > /dev/null; then
        PASSFAIL=Fail
        FAILED_TESTS="$FAILED_TESTS $scr"
        FAILURES=`expr $FAILURES + 1`
		if grep -i "interrupt" $TESTFILE > /dev/null; then
			PASSFAIL=Interrupted
		fi
    else
        PASSFAIL=Pass
    fi
    TOTALTESTS=`expr $TOTALTESTS + 1`

    REAL=`grep real $TIMEFILE | sed 's/real//'`
    USER=`grep user $TIMEFILE | sed 's/user//'`
    SYSS=`grep sys  $TIMEFILE | sed 's/sys//'`
    LOGLINE="{\"$scr\", $VERSION, \"`date "+%F %T"`\", $REAL, $USER, $SYSS, $PASSFAIL},"
    echo "$LOGLINE" | tee -a $LOGFILE

	REALTOT="$REALTOT+$REAL"
	USERTOT="$USERTOT+$USER"
	SYSSTOT="$SYSSTOT+$SYSS"
### exit

done

if [ $FAILURES -eq 0 ]; then
    PASSFAIL=Pass
else
    PASSFAIL=Fail
fi
REAL=`$CMD -i "Print(RoundTo($REALTOT,2));"`
USER=`$CMD -i "Print(RoundTo($USERTOT,2));"`
SYSS=`$CMD -i "Print(RoundTo($SYSSTOT,2));"`
LOGLINE="{\"Total\", $VERSION, \"`date "+%F %T"`\", $REAL, $USER, $SYSS, $PASSFAIL},"
echo "$LOGLINE" | tee -a $LOGFILE

rm $TESTFILE $TIMEFILE

# report

if [ $FAILURES -eq 0 ]; then
    RES="All $TOTALTESTS tests PASSED"
else
    if [ $FAILURES -eq 1 ]; then
        echo "Failed test: $FAILED_TESTS"
    else
        echo "Failed tests: $FAILED_TESTS"
    fi
    RES="$FAILURES of $TOTALTESTS tests FAILED"
fi
EQS=`echo $RES | sed 's/./=/g'`

echo "Command = \"$CMD\""
echo "Version = $VERSION"
echo "Date = \"`date "+%F %T"`\""
echo "Logfile = \"$LOGFILE\""
echo "/*$EQS" | tee -a $LOGFILE
if [ "x$FAILED_TESTS" != "x" ]; then
    echo "Failed = $FAILED_TESTS" | tee -a $LOGFILE
fi
echo " $RES " | tee -a $LOGFILE
echo "$EQS*/" | tee -a $LOGFILE

exit $FAILURES
